package com.mdd.common.config;


import cn.hutool.core.io.FileUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

@Slf4j
@Configuration
public class AlipayConfiguration {
    @Resource
    ProjectProperties projectProperties;
    @Resource
    ProjectWapProperties projectWapProperties;
    public static String privateKey;

    public static String wapPrivateKey;
    private static AlipayConfig alipayDefaultConfig;

    private static AlipayConfig alipayWapConfig;

    @Bean(name = "defaultAlipayClient")
    DefaultAlipayClient alipayConfig(){
        alipayDefaultConfig = new AlipayConfig();
        alipayDefaultConfig.setServerUrl(projectProperties.getAlipayServerUrl());
        alipayDefaultConfig.setAppId(projectProperties.getAlipayAppId());
        privateKey= FileUtil.readString(
                homeAddedPath(projectProperties.getAlipayPrivateKeyFile()),
                StandardCharsets.UTF_8
        );
        alipayDefaultConfig.setPrivateKey(privateKey);
        alipayDefaultConfig.setAppCertContent(FileUtil.readString(
                 homeAddedPath(projectProperties.getAlipayAppCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayDefaultConfig.setAlipayPublicCertContent(FileUtil.readString(
                 homeAddedPath(projectProperties.getAlipayAliPublicCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayDefaultConfig.setRootCertContent(FileUtil.readString(
                 homeAddedPath(projectProperties.getAlipayAliRootCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayDefaultConfig.setFormat("json");
        alipayDefaultConfig.setCharset("UTF-8");
        alipayDefaultConfig.setSignType("RSA2");
        try {
            return new DefaultAlipayClient(alipayDefaultConfig);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error("支付宝请求Client初始化失败");
        }
        return null;
    }

    @Bean(name = "defaultAlipayWapClient")
    DefaultAlipayClient alipayWapConfig(){
        alipayWapConfig = new AlipayConfig();
        alipayWapConfig.setServerUrl(projectWapProperties.getAlipayServerUrl());
        alipayWapConfig.setAppId(projectWapProperties.getAlipayAppId());
        wapPrivateKey= FileUtil.readString(
                homeAddedPath(projectWapProperties.getAlipayPrivateKeyFile()),
                StandardCharsets.UTF_8
        );
        alipayWapConfig.setPrivateKey(wapPrivateKey);
        alipayWapConfig.setAppCertContent(FileUtil.readString(
                homeAddedPath(projectWapProperties.getAlipayAppCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayWapConfig.setAlipayPublicCertContent(FileUtil.readString(
                homeAddedPath(projectWapProperties.getAlipayAliPublicCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayWapConfig.setRootCertContent(FileUtil.readString(
                homeAddedPath(projectWapProperties.getAlipayAliRootCertFile()),
                StandardCharsets.UTF_8
        ));
        alipayWapConfig.setFormat("json");
        alipayWapConfig.setCharset("UTF-8");
        alipayWapConfig.setSignType("RSA2");
        try {
            return new DefaultAlipayClient(alipayWapConfig);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error("支付宝请求Client初始化失败");
        }
        return null;
    }

    public static AlipayConfig getAlipayWapConfig() {
        return alipayWapConfig;
    }
    public static AlipayConfig getAlipayDefaultConfig() {
        return alipayDefaultConfig;
    }

    private static String homeAddedPath(String fileName){
        return System.getProperty("user.dir")+fileName;
    }
}
